/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufpr.tads.Rent.model;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Neti
 */
public class ClienteDAO {
    private final Connection connection;

    
    private static final String sqlselectcliente = "select * from cliente";
    private static final String sqlselectclientecpf = "select * from cliente where cpf = ?";
    private static final String sqlinsert = "insert into cliente (nome,sobrenome, endereco, cpf, rg)values(?,?,?,?,?)";
    private static final String sqlup= "update cliente set nome = ?,sobrenome=?,endereco = ?,rg = ?, cpf = ? where idcliente = ?";
    private static final String sqldel = "delete from cliente where idcliente=?";

    public ClienteDAO() throws IOException, FileNotFoundException, ClassNotFoundException, SQLException{
       this.connection = new ConnectionFactory().getConnection();
    }

    public void adicionaCliente(ClienteDTO cliente) throws SQLException{
        PreparedStatement stmt = connection.prepareStatement(sqlinsert); 
        stmt.setString(1, cliente.getNome());
        stmt.setString(2, cliente.getSobrenome());
        stmt.setString(3, cliente.getEndereco());
        stmt.setString(4, cliente.getCpf());
        stmt.setString(5, cliente.getRg());
        stmt.execute();
        stmt.close();
    }
    
    
    public List<ClienteDTO> CarregaCliente ()throws SQLException{
        List<ClienteDTO> lista = new ArrayList();
        PreparedStatement stmt = connection.prepareStatement(sqlselectcliente);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                ClienteDTO ct = new ClienteDTO();
                ct.setIdCliente(rs.getInt("idCliente"));
                ct.setNome(rs.getString("nome"));
                ct.setSobrenome(rs.getString("sobrenome"));
                ct.setEndereco(rs.getString("endereco"));
                ct.setCpf(rs.getString("cpf"));
                ct.setRg(rs.getString("rg"));
                lista.add(ct);
            } 
            stmt.close();
            rs.close();
        return lista;
    }
    
     public void updateCliente(ClienteDTO cliente) throws SQLException{
            PreparedStatement stmt = connection.prepareStatement(sqlup);
            stmt.setString(1,cliente.getNome());
            stmt.setString(2,cliente.getSobrenome());
            stmt.setString(3,cliente.getEndereco()); 
            stmt.setString(4,cliente.getRg());
            stmt.setString(5,cliente.getCpf());
            stmt.setInt(6, cliente.getIdCliente());
            stmt.executeUpdate();
           stmt.close();
        
    }
     public void delCliente(int id) throws SQLException{
               
    
      PreparedStatement stmt = connection.prepareStatement(sqldel);      
        stmt.setInt(1, id);
        stmt.executeUpdate();
        stmt.close();
        
      }
}

     

  
  
       
 
   
   
    

